let requestUrl;
let rarityArray = ["非珍品", "珍品"];

function addHandler() {
	requestUrl = "/prop/insert";

	$("#prop_dialog").dialog("open");

	$("#dataId").textbox("enable");
	$("#tableName").combobox("enable");
	$("#bindState").combobox("setValue", 1);
	$("#propId").combotreegrid("readonly", false);
}

function editHandler() {
	let rowData = $("#prop_list").datagrid("getSelected");
	
	if (rowData) {
		requestUrl = "/prop/updateById";

		$("#id").val(rowData.id);
		$("#rarity").combobox("setValue", rowData.rarity);
		$("#roleId").combobox("setValue", rowData.roleId);
		$("#days").numberspinner("setValue", rowData.days);
		$("#bindState").combobox("setValue", rowData.state);
		$("#quality").numberspinner("setValue", rowData.quality);
		$("#durability").numberspinner("setValue", rowData.durability);
		$("#dataId").textbox("setValue", rowData.dataId).textbox("disable");
		$("#propId").combotreegrid("setValue", rowData.propId).combotreegrid("readonly", true);
		$("#tableName").combobox("setValue", rowData.tableName).combobox("disable");

		$("#prop_dialog").dialog("open");
	} else {
		alertMsg("请选择要修改的记录！", "warning");
	}
}

function deleteHandler() {
	let rowData = $("#prop_list").datagrid("getSelected");

	if (rowData) {
		$.messager.confirm("提示", "是否确认删除？", function (bool) {
			if (bool) {
				ajaxPost("/prop/deleteById", {
					id: rowData.id
				}, function (resp) {
					showMsg(resp.message);

					$("#prop_list").datagrid("reload");
				}, error);
			}
		});
	} else {
		alertMsg("请选择要删除的记录！", "warning");
	}
}

function viewHandler() {
	let rowData = $("#prop_list").datagrid("getSelected");

	if (rowData) {
		let days = rowData.days;

		if (days !== 0) {
			if (days < 0) {
				days = "永久";
			}

			$("#frozen").show();
			$("#prop_days").html(days);
		} else {
			$("#frozen").hide();
		}

		$("#prop_name").html(rowData.name);
		$("#prop_note").html(rowData.note);
		$("#prop_type").html(rowData.type);
		$("#prop_quality").html(rowData.extraNote);
		$("#prop_image").attr("src", rowData.image);
		$("#icon-class").removeClass("bind zhenpin");

		let bindState = rowData.bindState;

		if (bindState === 2) {
			$("#icon-class").addClass("bind");
		} else {
			let rarity = rowData.rarity;

			if (rarity === 1) {
				$("#icon-class").addClass("zhenpin");
			}
		}

		// 耐久度的展示
		let durability = rowData.durability;

		if (durability == null) {
			$(".durability").hide();
			$("#prop_durability").empty();
		} else {
			$(".durability").show();
			$("#prop_durability").html(rowData.durability);
		}

		$("#prop_detail").dialog("open");
	} else {
		alertMsg("请选择要查看的记录！", "warning");
	}
}

$(document).ready(function() {

	$("#role_id").combobox({
		url: "/role_account/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		panelHeight: "auto",
		prompt: "-选择角色-",
		formatter: function(row) {
			ajaxGet("/role_category/selectById", {
				id: row.categoryId
			}, function(resp) {
				let data = resp.data;

				$("#role_" + row.id).attr({
					"title": data.name,
					"src": data.image,
					"alt": data.name
				});
			}, error);

			return "<img height='20' id='role_" + row.id + "' />&ensp;" + row.name;
		}
	});

	$("#search").linkbutton({
		iconCls: "icon-search"
	}).click(function() {
		let roleId = $("#role_id").combobox("getValue");

		$("#prop_list").datagrid("reload", {
			roleId: roleId
		});
	});

	$("#clear").linkbutton({
		iconCls: "icon-delete"
	}).click(function() {
		$("#search_form").form("clear");
	});

	/******************************************************************************************************/

	$("#propId").combotreegrid({
		url: "/prop_category/listComboTreeGrid",
		idField: "id",
		treeField: "name",
		textField: "name",
		width: 150,
		editable: true,
		required: true,
		panelWidth: 500,
		limitToGrid: true,
		onBeforeSelect: function (row) {
			let children = row.children;

			return !children || children.length === 0;
		},
		onSelect: function (row) {
			let parentId = row._parentId;

			$("#parentId").val(parentId);
		},
		columns:[[
			{field: "name", title: "道具名称", align: "left", width: 200},
			{field: "image", title: "道具图片", align: "center", width: 100
				, formatter: function(value) {
					if(value) {
						return "<img height='" + (size / 2) + "' src='" + value + "' />";
					}
				}
			},
			{field: "note", title: "道具说明", align: "center", width: 100}
		]]
	});

	$("#quality").numberspinner({
		min: 1,
		width: 150,
		editable: true,
		required: false
	});

	fetchSelect("#rarity", rarityArray, {
		width: 150,
		required: true
	});

	$("#bindState").combobox({
		url: "/bind_state/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		required: true,
		panelHeight: "auto",
		onSelect: function (record) {
			let days = record.days;

			if (days <= 0) {
				$("#days").numberspinner("setValue", days);
			}
		}
	});

	$("#days").numberspinner({
		min: -1,
		max: 60,
		width: 150,
		required: true
	});

	$("#durability").numberspinner({
		min: 0,
		width: 150
	});

	$("#tableName").combobox({
		url: "/table/selectAll",
		valueField: "name",
		textField: "name",
		width: 150,
		panelWidth: 280,
		required: false,
		panelHeight: "auto"
	});

	$("#dataId").textbox({
		width: 150,
		required: false
	});

	$("#roleId").combobox({
		url: "/role_account/selectAll",
		valueField: "id",
		textField: "name",
		width: 150,
		required: true,
		panelHeight: "auto",
		formatter: function(row) {
			ajaxGet("/role_account/selectRoleDetails", {
				id: row.id
			}, function(resp) {
				let data = resp.data;

				$("#role_modeling_" + row.id).attr({
					"src": data.roleImage,
					"title": data.name,
					"alt": data.name
				});
				$("#role_school_" + row.id).attr({
					"src": data.schoolImage,
					"title": data.school,
					"alt": data.school
				});
			}, error);

			return "<img height='20' id='role_modeling_" + row.id + "' />&ensp;" + row.name
				+ "&ensp;<img height='20' id='role_school_" + row.id + "' />";
		}
	});

	$("#prop_dialog").dialog({
		title: "道具信息",
		width: 500,
		buttons: [{
			iconCls: "icon-save",
			text: "保存",
			handler: function() {
				let selector = "#prop_form";

				checkForm(selector, function () {
					let data = $(selector).serialize();

					ajaxPost(requestUrl, data, function(resp) {
						showMsg(resp.message);

						$(selector).form("clear");
						$("#prop_dialog").dialog("close");
						$("#prop_list").datagrid("reload");
					}, error);
				});
			}
		}, {
			iconCls: "icon-cancel",
			text: "取消",
			handler: function() {
				$("#prop_form").form("clear");
				$("#prop_dialog").dialog("close");
			}
		}]
	});

	/******************************************************************************************************/

	$("#prop_detail .splitter").textbox({
		width: 200
	});

	$("#prop_detail").dialog({
		title: " ",
		width: 220
	});

	/******************************************************************************************************/

	/**
	 * 表格体的高度
	 * @type {number}
	 */
	let heigt_body = heigt_tr_with_role_details * 10;
	/**
	 * 表格高度
	 * @type {number}
	 */
	let height_table = heigt_tool + heigt_header + heigt_body + heigt_footer + heigt_scoll_bar + 2;

	$("#prop_list").datagrid({
		url: "/prop/selectByPage",
		method: "get",
		height: height_table,
		rownumbers: true,
		pagination: true,
		fitColumns: false,
		toolbar: [{
			iconCls: "icon-add",
			text: "添加",
			handler: function() {
				addHandler();
			}
		}, "-", {
			iconCls: "icon-edit",
			text: "修改",
			handler: function() {
				editHandler();
			}
		}, "-", {
			iconCls: "icon-delete",
			text: "删除",
			handler: function() {
				deleteHandler();
			}
		}, "-", {
			iconCls: "icon-search",
			text: "查看道具",
			handler: function() {
				viewHandler();
			}
		}],
		frozenColumns: [[
			{field: "id", hidden: true},
			{field: "propId", title: "道具类型", align: "center", width: 160
				, formatter: function(value, rowData, rowIndex) {
					// ajaxGet("/prop/selectProp", {
					// 	propId: value
					// }, function(resp) {
					// 	let data = resp.data;
					//
					// 	$("#prop_" + rowIndex).attr({
					// 		"title": data.name,
					// 		"src": data.image
					// 	});
					// }, error);

					// return "<img height='" + size + "' id='prop_" + rowIndex + "' />";
					return "<img height='" + size + "' title='" + rowData.name + "' src='" + rowData.image + "' />";
				}
			},
			{field: "state", title: "道具状态", align: "center", width: 160
				, formatter: function(value, rowData, rowIndex) {
					ajaxGet("/prop_state/selectById", {
						id: value
					}, function(resp) {
						let data = resp.data;

						$("#state_" + rowIndex).html(data.name)
							.attr("title", data.note);
					}, error);

					return "<div id='state_" + rowIndex + "'></div>";
				}
			},
			roleDetailsColumn
		]],
		columns: [[
			{field: "quality", title: "道具品质", align:"center", width: 160, sortable: true
				, formatter: function(value) {
					return value ? value : "/";
				}
			},
			{field: "bindState", title: "绑定状态", align: "center", width: 160
				, formatter: function(value, rowData, rowIndex) {
					if (!value) {
						return "<div> 无</div>";
					} else {
						ajaxGet("/bind_state/selectById", {
							id: value
						},function(resp) {
							let data = resp.data;

							$("#bind_state_" + rowIndex).html(data.name);
						}, error);

						return "<div id='bind_state_" + rowIndex + "'></div>";
					}
				}
			},
			{field: "days", title: "冻结天数", align: "center", width: 160
				, formatter: function(value) {
					if (value === -1) {
						return "<div class='red'>摆摊永久冻结</div>";
					} else if (value === 0) {
						return "<div>/</div";
					} else {
						return "冻结<span class='red'>" + value + "</span>天";
					}
				}
			},
			{field: "tableName", title: "数据库表", align:"center", width: 160
				, formatter: function(value) {
					return value ? value : "/";
				}
			},
			{field: "dataId", title: "数据编号", align:"center", width: 160
				, formatter: function(value) {
					return value ? value : "/";
				}
			},
			{field: "location", title: "存放位置", align:"center", width: 160
				, formatter: function(value) {
					return value ? value : "/";
				}
			}
		]]
	});

});